/*
 * @lc app=leetcode.cn id=面试题 04.02 lang=cpp
 * @lcpr version=30204
 *
 * [面试题 04.02] 最小高度树
 */

// @lcpr-template-start
using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

// @lcpr-template-end

// @lc code=start

class Solution
{
public:
    TreeNode *sortedArrayToBST(vector<int> &nums)
    {
        return buildTree(nums.begin(), nums.end());
    }
    TreeNode *buildTree(vector<int>::iterator be, vector<int>::iterator en)
    {
        if (be >= en)
            return nullptr;
        auto mid = (en - be) / 2 + be;
        TreeNode *root = new TreeNode;
        root->val = *mid;
        root->left = buildTree(be, mid);
        root->right = buildTree(mid + 1, en);
        return root;
    }
};
// @lc code=end
